package com.house.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.house.common.context.BaseContext;
import com.house.model.dto.CollectChangeDto;
import com.house.model.entity.Collect;
import com.house.model.vo.HouseVo;
import com.house.service.mapper.CollectMapper;
import com.house.service.service.ICollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Override
    public boolean isCollectByHouseId(String houseId) {
        String userId = BaseContext.getCurrentId();
        Collect collect = collectMapper.selectCollectByUserIdAndHouseId(userId, houseId);
        return collect != null;
    }

    @Override
    public void changeCollect(CollectChangeDto dto) {
        String userId = BaseContext.getCurrentId();
        if (dto.getIs()) {
            Collect collect = Collect.builder()
                    .userId(userId)
                    .collectId(UUID.randomUUID().toString())
                    .houseId(dto.getHouseId())
                    .createDate(new Date(System.currentTimeMillis()))
                    .createPerson(userId)
                    .build();
            collectMapper.insert(collect);
        }else {
            LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Collect::getUserId,userId);
            queryWrapper.eq(Collect::getHouseId,dto.getHouseId());
            collectMapper.delete(queryWrapper);
        }
    }

    @Override
    public int getCollectCount() {
        String userId = BaseContext.getCurrentId();
        int i = collectMapper.selectCollectCountByUserId(userId);
        return i;
    }

    @Override
    public List<HouseVo> selectCollectByUserId() {
        String userId  = BaseContext.getCurrentId();
        List<HouseVo> houseVos = collectMapper.selectCollectByUserId(userId);
        return houseVos;
    }


}
